/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package acousticfield3d.gui;

import acousticfield3d.gui.panels.MovePanel;
import acousticfield3d.utils.Parse;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JTextField;

/**
 *
 * @author am14010
 */
public class KeyboardControllerForm extends javax.swing.JFrame {
    final MainForm mf;
    final List<Channel> channels = new ArrayList<>();
    public JButton commitButton = null;
    
    public KeyboardControllerForm(MainForm mf) {
        this.mf = mf;
        initComponents();
        
        configChannels();
    }

    private void configChannels(){
        final MovePanel mp = mf.movePanel;
        label1Text.setText("Periods");
        channels.add( new Channel('u', 'j', mp.gettUpText(), null, gain1Text));
        
        label2Text.setText("Offset");
        channels.add( new Channel('i', 'k', mp.getpUpText(), null, gain2Text));
        
        label3Text.setText("Position");
        channels.add( new Channel('o', 'l', mp.getUpButton(), mp.getDownButton(), gain3Text));
        
        commitButton = mf.movePanel.getNeutralButton();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label1Text = new javax.swing.JLabel();
        gain1Text = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        label2Text = new javax.swing.JLabel();
        gain2Text = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        label3Text = new javax.swing.JLabel();
        gain3Text = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        inputText = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Keyboard Controller");

        label1Text.setText("Val1");

        gain1Text.setText("1");

        jLabel2.setText("U, J");

        label2Text.setText("Val2");

        gain2Text.setText("0.0005");

        jLabel4.setText("I,K");

        label3Text.setText("Val3");

        gain3Text.setText("0.0005");

        jLabel6.setText("O,L");

        inputText.setText("Just type here");
        inputText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                inputTextKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(inputText)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(label2Text, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                            .addComponent(label1Text, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(label3Text, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(gain3Text, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)
                            .addComponent(gain1Text)
                            .addComponent(gain2Text))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label1Text)
                    .addComponent(gain1Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label2Text)
                    .addComponent(gain2Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label3Text)
                    .addComponent(gain3Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(inputText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void inputTextKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_inputTextKeyPressed
        final char k = evt.getKeyChar();
        boolean shouldCommit = false;
        
        for(Channel c : channels){
            if (k == c.keyUp || k == c.keyDown){
                shouldCommit = true;
                if (k == c.keyUp && c.component instanceof JButton){
                    pressButton((JButton) c.component);
                }else if (k == c.keyDown && c.component2 instanceof JButton){
                    pressButton((JButton) c.component2);
                }else if(c.component instanceof JTextField){
                    final JTextField text = (JTextField)c.component;
                    final float gain = Parse.toFloat( c.gain.getText() );
                    final float sGain = (k == c.keyDown) ? -gain : gain;
                    final float newValue = Parse.toFloat( text.getText() ) +  sGain;
                    text.setText( newValue + "");
                }
            }
        }
        
        if (shouldCommit && commitButton != null){
            pressButton(commitButton);
        }
    }//GEN-LAST:event_inputTextKeyPressed

    private static void pressButton(final JButton button){
        for (ActionListener al : button.getActionListeners()) {
            al.actionPerformed(null);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField gain1Text;
    private javax.swing.JTextField gain2Text;
    private javax.swing.JTextField gain3Text;
    private javax.swing.JTextField inputText;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel label1Text;
    private javax.swing.JLabel label2Text;
    private javax.swing.JLabel label3Text;
    // End of variables declaration//GEN-END:variables


    public class Channel{
        public char keyUp, keyDown;
        public JComponent component;
        public JComponent component2;
        public JTextField gain;

        public Channel() {
        }

        public Channel(char keyUp, char keyDown, JComponent component, JComponent component2, JTextField gain) {
            this.keyUp = keyUp;
            this.keyDown = keyDown;
            this.component = component;
            this.component2 = component2;
            this.gain = gain;
        }
        
    }
    
}
